package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.ContentNews;
import cn.net.shuxing.yifu.common.bean.ContentSys;
import cn.net.shuxing.yifu.common.constant.OssConstant;
import cn.net.shuxing.yifu.common.dto.DateTimeVo;
import cn.net.shuxing.yifu.common.mapper.ContentNewsMapper;
import cn.net.shuxing.yifu.common.util.CommonExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.net.shuxing.yifu.common.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ContentNewsService extends ContentService<ContentNews, ContentNewsMapper> {

    @Autowired
    private OssCommonService ossCommonService;

    @Autowired
    private ContentNewsMapper contentNewsMapper;

    @Override
    protected void complete(ContentNews bean) {
        if (bean == null)
            return;
        LocalDateTime createTime = bean.getCreateTime();
        DateTimeVo dateTimeVo = new DateTimeVo(createTime);
        bean.setDateTimeVo(dateTimeVo);

        if (StringUtils.hasText(bean.getNewsDetail())) {
            bean.setNewsLinkUrl(ossCommonService.getUrl(OssConstant.ossBucketName, bean.getNewsDetail()));
        }
    }

    public ContentNews getByInlienLink(String link) {
        CommonExample example = new CommonExample(ContentSys.class);
        example.equals("newsLink", link);
        return getByExampleWithoutComplete(example);
    }

    public PageInfo<ContentNews> getPageNews(int pageNum, int pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<ContentNews> contentNewsList = contentNewsMapper.selectAll();
        PageInfo<ContentNews> pageInfo = new PageInfo<>(contentNewsList);
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }
}
